﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using EYSLibrary;
using System.Data.Entity;

namespace EYayincilikSistemi
{
    public partial class MakaleDegerlendir : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
                int davetId;
                if (Request.QueryString["DavetId"] != null && Int32.TryParse(Request.QueryString["DavetId"], out davetId))
                {
                    gridViewPanel.Visible = false;
                    anketPanel.Visible = true;
                    DavetiGetir(davetId);
                }
                else
                {
                    gridViewPanel.Visible = true;
                    anketPanel.Visible = false;
                    DavetleriGetir();
                }
            }
        }
        public void DavetleriGetir()
        {
            using (EYayincilikDBEntities entities = new EYayincilikDBEntities())
            {
                var result = from d in entities.Davet
                             where d.HakemId == 1     //Burada deneme amaçlı 1 verildi, 'hakemId' olması gerekiyor
                             select new
                             {
                                 YazarAdi = (d.Makale.Kullanici.Ad + " " + d.Makale.Kullanici.Soyad),
                                 MakaleBaslik = d.Makale.Baslik,
                                 EditorAdi = (d.Dergi.Kullanici.Ad + " " + d.Dergi.Kullanici.Soyad),
                                 d.DavetId
                             };
                DavetGridView.DataSource = result;
                DavetGridView.DataBind();
            }
        }
        public void DavetiGetir(int davetId)
        {
            using (EYayincilikDBEntities entities = new EYayincilikDBEntities())
            {
                var result = from d in entities.Davet
                             where d.DavetId == davetId
                             select d;
                Davet davet = result.FirstOrDefault();
                lblBaslik.Text = davet.Makale.Baslik;
                lblDergi.Text = davet.Dergi.DergiAdi;
                lblEditor.Text = davet.Dergi.Kullanici.Ad + " " + davet.Dergi.Kullanici.Soyad;
                lblMetin.Text = davet.Makale.Metin;
                lblYazar.Text = davet.Makale.Kullanici.Ad + " " + davet.Makale.Kullanici.Soyad;
            }
        }

        protected void DavetGridView_RowCommand(object sender, GridViewCommandEventArgs e)
        {
            int rowIndex = int.Parse(e.CommandArgument.ToString());
            int davetId = Int32.Parse(DavetGridView.DataKeys[rowIndex][0].ToString());
            if (e.CommandName == "Incele")
            {
                Response.Redirect("~/MakaleDegerlendir.aspx?DavetId=" + davetId);
            }
        }
    }
}